package com.nexus.service.sys.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nexus.core.exception.ServiceException;
import com.nexus.core.service.BaseService;
import com.nexus.core.sys.entity.SysRoleMenu;
import com.nexus.core.sys.dto.SysRoleMenuDTO;
import com.nexus.core.utils.ObjectUtils;
import com.nexus.service.sys.mapper.SysRoleMenuMapper;
import com.nexus.service.sys.service.mapstruct.SysRoleMenuWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 角色权限Service
 *
 * @author 路正宁
 * @version 2023-03-16
 */
//@DS("zxhy_platform_db")
//@Service
@Transactional(rollbackFor = {ServiceException.class,Exception.class, RuntimeException.class})
public class SysRoleMenuService extends BaseService<SysRoleMenuMapper, SysRoleMenu,SysRoleMenuDTO,SysRoleMenuWrapper> {

    @Autowired
    SysRoleMenuWrapper sysRoleMenuWrapperImpl;
    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public SysRoleMenuDTO findById(String id) {
        return baseMapper.findById(id);
    }

    /**
     * 查询角色下的菜单
     * @Date 2024/6/11 15:33
     * @Author luzhengning
     **/
    public List<SysRoleMenuDTO> findByRole(String roleId){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("a.sys_role",roleId);
        IPage<SysRoleMenuDTO> page = findPage(new Page<>(1, 5000), queryWrapper);
        if(page!=null&&page.getRecords()!=null&&page.getRecords().size()>0){
            return page.getRecords();
        }
        return null;
    }

    /**
     * 自定义分页检索
     *
     * @param page
     * @param queryWrapper
     * @return
     */
    public IPage<SysRoleMenuDTO> findPage(Page<SysRoleMenuDTO> page, QueryWrapper queryWrapper) {
        queryWrapper.eq("a.deleted", 0); // 排除已经删除
        queryWrapper.eq("sysMenu.deleted", 0); // 排除已经删除
        return baseMapper.findList(page, queryWrapper);
    }

    /**
     * 删除角色关联
     * @Date 2023/9/6 10:04
     * @Author luzhengning
     **/
    public boolean deleteByRoleIds(List<String> roleIds){
        if(ObjectUtils.isEmpty(roleIds))return false;
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.in("sys_role",roleIds);
        return this.remove(queryWrapper);
    }
}
